package com.mycompany.app;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component("loggingAspect")
@Aspect
public class LoggingAspect {

	Log log = LogFactory.getLog(this.getClass().getSimpleName());
	
	@Pointcut("execution(public * com.mycompany.app.IAnimalService.*(..)) && args(animal)")
	public void loggingPointcutWithAnimal(Animal animal){
		
	}
	
	@Pointcut("execution(public * com.mycompany.app.IAnimalService.*(..)) && args(animalId)")
	public void loggingPointcutWithInt(int animalId){
		
	}
	
//	@Pointcut("execution(* com.mycompany.app.IAnimalService.*.*(..))")
//	@Pointcut("execution(* com.mycompany.app.IAnimalService.*(..))")
//	public void execAOP(){};
	
//	@After("execution(public * *(..))")
//	@Before("com.mycompany.app.insertAnimal(..)")
//	@Pointcut("execution(* com.mycompany.app.IAnimalService.*(..))")
//	@Before("execution(public * com.mycompany.app.IAnimalService.*(..))")
//	@After("execution(public * com.mycompany.app.IAnimalService.*(..))")
	
//	@Before(value="execAOP()")
//	public void doSomething(){
//		System.err.println("asdasd asdasdas");
//	}
	
	@Around("com.mycompany.app.LoggingAspect.loggingPointcutWithAnimal(animal)")
//	@Around("execution(public * com.mycompany.app.IAnimalService.*(..))")
	protected Object log(ProceedingJoinPoint pjp, Animal animal) throws Throwable{
		log.debug("LoggingAspect.log before animal " + animal);
		// start stopwatch
	    Object retVal = pjp.proceed();
	    // stop stopwatch
	    log.debug("LoggingAspect.log after animal " + pjp.getSignature().getName());
	    return retVal;

	}
	
	@Around("com.mycompany.app.LoggingAspect.loggingPointcutWithInt(animalId)")
//	@Around("execution(public * com.mycompany.app.IAnimalService.*(..))")
	protected Object log(ProceedingJoinPoint pjp, int animalId) throws Throwable{
		log.debug("LoggingAspect.log before animalId " + animalId);
		// start stopwatch
	    Object retVal = pjp.proceed();
	    // stop stopwatch
	    log.debug("LoggingAspect.log after animalId " + pjp.getSignature().getName());
	    return retVal;

	}
}
